package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wanmait.baohan.mapper.ReimbursementApplicationMapper;

import com.wanmait.baohan.pojo.ReimbursementApplication;

import com.wanmait.baohan.service.*;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import javax.annotation.Resource;
import java.util.List;
/*
周勇
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReimbursementServiceImpl implements ReimbursementService {

    @Resource
    private ReimbursementApplicationMapper reimbursementApplicationMapper;

    @Resource
    private BookingTicketApplicationService bookingTicketApplicationService;
    @Resource
    private BookingHotelService bookingHotelService;
    @Resource
    private BookingCountService bookingCountService;

    //查出符合条件并分页报销申请
    @Override
    public PageInfo<ReimbursementApplication> selectReimbursementApplication(Integer page, Integer limit, ReimbursementApplication reimbursementApplication, Date beginDate, Date endDate) {
        PageHelper.startPage(page, limit);
        List<ReimbursementApplication> reimbursementApplications = reimbursementApplicationMapper.selectByStaffName(reimbursementApplication,beginDate, endDate);
        PageInfo<ReimbursementApplication> pageInfo = new PageInfo<>(reimbursementApplications,7);
        return pageInfo;
    }

    @Override
    public ReimbursementApplication verificationTravel() {
        return reimbursementApplicationMapper.verificationTravel();
    }

    @Override
    public ReimbursementApplication selectByIdReimbursement(Integer id,Integer enable) {
        return  reimbursementApplicationMapper.selectByPrimaryKey(id,enable);
    }

    //周勇提交报销申请
    @Override
    public void addReimbursementService(ReimbursementApplication reimbursementApplication) {
       // System.out.println("impl提交");
        reimbursementApplication.setProcessNodeId(14);
        reimbursementApplication.setEnable(1);
        Integer userid=reimbursementApplication.getStaff().getId();
        bookingTicketApplicationService.selectNode(userid);
       // System.out.println("人的id"+userid);
        //System.out.println("申请表id"+reimbursementApplication.getTravelApplicationId());
        Integer id=reimbursementApplicationMapper.insert(reimbursementApplication);
       // System.out.println("impl提交成功");

        Integer userId=reimbursementApplication.getStaff().getId();
        Integer tavalId=reimbursementApplication.getTravelApplicationId();

        if(reimbursementApplication.getDictionary().getId()==19) {
            bookingCountService.updateByAccount(userId, tavalId);
        }else if(reimbursementApplication.getDictionary().getId()==38){
            bookingHotelService.updateByHotel(userId,tavalId);
        }

    }
    //周勇报销结果写入
    @Override
    public void reimbursementResult(ReimbursementApplication reimbursementApplication) {
        System.out.println("进入方法");
        if(reimbursementApplication.getEnable()==0) {
            if(reimbursementApplication.getDictionary().getId()==19){
               // System.out.println("applicationId=========="+reimbursementApplication.getTravelApplicationId());
                bookingTicketApplicationService.updateByIdState(reimbursementApplication.getTravelApplicationId(),3);
            }else if(reimbursementApplication.getDictionary().getId()==38){
               // System.out.println("applicationId=========="+reimbursementApplication.getTravelApplicationId());
                bookingHotelService.updateByHotel(reimbursementApplication.getTravelApplicationId(),3);
            }
        }else if(reimbursementApplication.getEnable()==2)
        {
            if(reimbursementApplication.getDictionary().getId()==19){
                bookingTicketApplicationService.updateByIdState(reimbursementApplication.getTravelApplicationId(),5);
            }else if(reimbursementApplication.getDictionary().getId()==38){
                bookingHotelService.updateByHotel(reimbursementApplication.getTravelApplicationId(),5);
            }
        }
        reimbursementApplicationMapper.updateByPrimaryKeySelective(reimbursementApplication);
       // System.out.println("结束方法");
    }

    @Override
    public void updateReimbursementService(Integer id,Integer enable) {
        reimbursementApplicationMapper.updateRe(id,enable);
    }
}
